package game

import (
	"context"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"go.uber.org/zap"
	"google.golang.org/grpc/status"
	"qm_system_server/server/global"
	"qm_system_server/server/model/common/response"
	"qm_system_server/server/proto/grpc/version"
)

type QmVersionApi struct {
}

func (QmVersionApi *QmVersionApi) CreateQmVersion(c *gin.Context) {
	type request struct {
		GameHash   string `json:"game_hash" from:"game_hash"`
		PlatformId int64  `json:"platform_id" form:"platform_id"`
		Name       string `json:"name" form:"name"`
		ServerIds  string `json:"server_ids_str" form:"server_ids_str"`
	}
	var req request
	_ = c.ShouldBindJSON(&req)

	//获取header中的game-id
	req.GameHash = c.Request.Header.Get("game-hash")
	req.PlatformId = cast.ToInt64(c.Request.Header.Get("platform-id"))

	if req.Name == "" || req.ServerIds == "" || req.PlatformId == 0 || req.GameHash == "" {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	client := version.NewVersionServiceClient(global.ServerGrpc)

	_, err := client.Create(context.Background(), &version.CreateRequest{
		Version: &version.Version{
			GameHash:   req.GameHash,
			PlatformId: req.PlatformId,
			Name:       req.Name,
			ServerIds:  req.ServerIds,
		},
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}
	response.OkWithMessage("创建成功", c)
}

func (QmVersionApi *QmVersionApi) DeleteQmVersion(c *gin.Context) {
	ID := c.Query("id")

	//转成int64
	id := cast.ToInt64(ID)

	client := version.NewVersionServiceClient(global.ServerGrpc)

	_, err := client.Delete(context.Background(), &version.DeleteRequest{
		Id: id,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}
	response.OkWithMessage("删除成功", c)
}

func (QmVersionApi *QmVersionApi) DeleteQmVersionByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	if err := qmServerService.DeleteQmServerByIds(IDs); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

func (QmVersionApi *QmVersionApi) UpdateQmVersion(c *gin.Context) {
	type request struct {
		Id        int64  `json:"id" form:"id"`
		Name      string `json:"name" form:"name"`
		ServerIds string `json:"server_ids_str" form:"server_ids_str"`
	}

	var req request

	_ = c.ShouldBindJSON(&req)

	if req.Id == 0 || req.Name == "" || req.ServerIds == "" {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	grpcClient := version.NewVersionServiceClient(global.ServerGrpc)

	_, err := grpcClient.Update(context.Background(), &version.UpdateRequest{
		Version: &version.Version{
			Id:        req.Id,
			Name:      req.Name,
			ServerIds: req.ServerIds,
		},
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithMessage("更新成功", c)
}

func (QmVersionApi *QmVersionApi) FindQmVersion(c *gin.Context) {
	type request struct {
		Id int64 `json:"id" form:"id"`
	}

	var req request

	c.ShouldBindQuery(&req)

	//判断不能为空
	if req.Id == 0 {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	grpcClient := version.NewVersionServiceClient(global.ServerGrpc)
	getResp, err := grpcClient.Get(context.Background(), &version.GetRequest{
		Id: req.Id,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithData(getResp.Version, c)
}

func (QmVersionApi *QmVersionApi) GetQmVersionList(c *gin.Context) {
	type request struct {
		Page       int64  `json:"page" form:"page"`
		PlatformId int64  `json:"platformId" form:"platform_id"`
		GameHash   string `json:"gameHash" form:"game_hash"`
		PageSize   int64  `json:"pageSize" form:"pageSize"`
	}

	var req request

	_ = c.ShouldBindQuery(&req)

	req.GameHash = c.Request.Header.Get("game-hash")
	req.PlatformId = cast.ToInt64(c.Request.Header.Get("platform-id"))

	grpcClient := version.NewVersionServiceClient(global.ServerGrpc)

	version, err := grpcClient.List(context.Background(), &version.ListRequest{
		Platform: req.PlatformId,
		Page:     req.Page,
		GameHash: req.GameHash,
		PageSize: req.PageSize,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithData(response.PageResult{
		List:  version.Version,
		Total: version.Total,
		Page:  int(req.Page),
	}, c)

}
